﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Thaire.Investment.Common;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class FixedIncomeGuaranteeModel
    {
        public FixedIncomeGuaranteeModel()
        {
            ObligationList = new BindingList<SelectListItem>();
            ObligationNoList = new BindingList<SelectListItem>();
            TransactionType = "Guarantee";
            KeepAtList = new BindingList<SelectListItem>();
            PhysicalList = new BindingList<SelectListItem>();

        }
        public FixIncomeSecuritiesTransactionScreenData ScreenData { set; get; }
        public AddFixedIncomePurchaseModel PurchaseModel { set; get; }
        public IList<SelectListItem> ObligationList { set; get; }
        public IList<SelectListItem> ObligationNoList { set; get; }
        public IList<SelectListItem> PhysicalList { get; set; }
        public IList<SelectListItem> KeepAtList { get; set; }


        public String SelectedPhysical { set; get; }
        public String SelectedKeepAt { set; get; }




        [Required(ErrorMessage = "โปรดระบุ")]
        public String SelectedObligation { set; get; }
        [Required(ErrorMessage = "โปรดระบุ")]
        public String SelectedObligationNo { set; get; }

        public String TransactionType { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [Display(Name = "ประเภทหลักประกัน")]
        public String GuaranteeType { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd'/'MM'/'yyyy}")]
        [Display(Name = "Start Date")]
        public DateTime? StartDate { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd'/'MM'/'yyyy}")]
        [Display(Name = "End Date")]
        public DateTime? EndDate { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        [Range(1, (double)decimal.MaxValue, ErrorMessage = "ข้อมูลต้อง >= 1")]
        public int? Unit { set; get; }


        public  int? BookCount { set; get; }
        public String BookNo { set; get; }


        public String BtnSave { set; get; }
        public String BtnCancel { set; get; }
        public bool UpdateMode { get; set; }
        public String TransactionID { get; set; }
        public String PurcheseID { get; set; }
        public String HeaderID { get; set; }

        public void SetFixIncomeSecuritiesTransaction(FixIncomeSecuritiesTransaction tran)
        {
            HeaderID = tran.FixincomesecuritiesHeaderID.ToString();
            PurcheseID = tran.PurchaseID.ToString();
            TransactionID = tran.TransactionID.ToString();
            TransactionType = tran.TransactionType;
            SelectedObligation = tran.Obligation;
            SelectedObligationNo = tran.ObligationNO;
            StartDate = tran.GuaranteeStartDate;
            EndDate = tran.GuaranteeEndDate;
            GuaranteeType = tran.GuaranteeType;
            if (tran.Unit.HasValue)
                Unit = tran.Unit.Value;

            BookCount = tran.BookCount;
            BookNo = tran.BookNo;
            SelectedPhysical = tran.Physical;
            SelectedKeepAt = tran.KeepAt;
        }
        public FixIncomeSecuritiesTransaction ToFixIncomeSecuritiesTransaction()
        {
            var tran = new FixIncomeSecuritiesTransaction();
            tran.FixincomesecuritiesHeaderID = DataConverter.ConvertStringToIntNull(HeaderID).GetValueOrDefault();
            tran.PurchaseID = DataConverter.ConvertStringToIntNull(PurcheseID).GetValueOrDefault();
            tran.TransactionID = DataConverter.ConvertStringToIntNull(TransactionID).GetValueOrDefault();
            tran.TransactionType = TransactionType;
            tran.Unit = Unit.GetValueOrDefault();
            tran.GuaranteeStartDate = StartDate;
            tran.GuaranteeEndDate = EndDate;
            tran.Obligation = SelectedObligation;
            tran.ObligationNO = SelectedObligationNo;
            tran.GuaranteeType = GuaranteeType;
            tran.BookCount = BookCount;
            tran.BookNo = BookNo;
            tran.Physical = SelectedPhysical;
            tran.KeepAt = SelectedKeepAt;
            return tran;
        }
        public new static FixedIncomeGuaranteeModel Build(FixIncomeSecuritiesTransactionScreenData data)
        {
            var model = new FixedIncomeGuaranteeModel { ScreenData = data };
            model.ObligationList.Add(WebUtil.SelectListSelectDefault());
            model.ObligationNoList.Add(WebUtil.SelectListSelectDefault());
            model.KeepAtList.Add(WebUtil.SelectListSelectDefault());
            model.PhysicalList.Add(WebUtil.SelectListSelectDefault());

            model.PurchaseModel = new AddFixedIncomePurchaseModel(data.FixincomesecuritiesPurchaseDTO.Header) { Purchase = data.FixincomesecuritiesPurchaseDTO.Purhcase };

            foreach (var item in data.Obligations)
            {
                model.ObligationList.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() });
            }
            foreach (var item in data.ObligationNOs)
            {
                model.ObligationNoList.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() });
            }

            foreach (var item in data.KeepAT)
            {
                model.KeepAtList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ValueEN.ToString() });
            }

            foreach (var item in data.Physical)
            {
                model.PhysicalList.Add(new SelectListItem() { Text = item.ValueEN, Value = item.ValueEN.ToString() });
            }

            model.PurcheseID = model.PurchaseModel.Purchase.FixincomesecuritiesPurchaseId.ToString();
            model.HeaderID = model.PurchaseModel.HeaderID.ToString();

            if (data.FixIncomeSecuritiesTransactionDTO != null && data.FixIncomeSecuritiesTransactionDTO.FixIncomeSecuritiesTransaction != null)
            {
                model.SetFixIncomeSecuritiesTransaction(data.FixIncomeSecuritiesTransactionDTO.FixIncomeSecuritiesTransaction);
            }
            return model;
        }





    }
}